Skip to content

Add ECS0005: prefer formattablestring for culture specific strings #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

rjmurillo
Copy link
Owner

@rjmurillo rjmurillo commented Aug 10, 2024

This pull request introduces a new Roslyn analyzer and associated code fix provider to enforce best practices for culture-specific string formatting in C#. The analyzer identifies cases where FormattableString or string.Create should be used instead of string for interpolated strings, depending on the target framework version. The code fix provider automatically suggests the appropriate replacement.

Changes

  • Analyzer: FormattableStringForCultureSpecificStringsAnalyzer
    • Detects culture-specific string usage and recommends either FormattableString, string.Create, or string.Format based on the .NET version.
  • Code Fix Provider: FormattableStringForCultureSpecificStringsCodeFixProvider
    • Provides automated fixes based on the analyzer's recommendations.
  • Tests: Comprehensive unit tests covering various scenarios, including different .NET versions and complex string interpolation cases.

This analyzer aligns modern C# and .NET practices, particularly Stephen Toub's guidance on using string.Create in .NET 6+

@rjmurillo rjmurillo added documentation Improvements or additions to documentation enhancement New feature or request analyzers build dependencies Pull requests that update a dependency file releasable github_actions labels Aug 10, 2024
@rjmurillo rjmurillo changed the base branch from main to release/v0.1.0 August 10, 2024 01:58
@rjmurillo rjmurillo added .NET and removed build dependencies Pull requests that update a dependency file releasable github_actions labels Aug 10, 2024
@rjmurillo rjmurillo added this to the v0.1.0 milestone Aug 10, 2024
Copy link

codacy-production bot commented Aug 12, 2024

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
-1.47% (target: -1.00%) 86.57%
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (c5a55a1) 595 548 92.10%
Head commit (c99180a) 811 (+216) 735 (+187) 90.63% (-1.47%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#49) 216 187 86.57%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

Codacy stopped sending the deprecated coverage status on June 5th, 2024. Learn more

@rjmurillo rjmurillo force-pushed the feature/issue-29-prefer-formattablestring-for-culture-specific-strings branch from cc6f104 to c8a3ce1 Compare August 12, 2024 18:00
@rjmurillo rjmurillo added build dependencies Pull requests that update a dependency file labels Aug 13, 2024
@rjmurillo rjmurillo force-pushed the feature/issue-29-prefer-formattablestring-for-culture-specific-strings branch from 7977558 to 8ca44f8 Compare August 13, 2024 21:50
@rjmurillo rjmurillo force-pushed the feature/issue-29-prefer-formattablestring-for-culture-specific-strings branch from 5601654 to efbeb91 Compare August 13, 2024 21:58
@rjmurillo rjmurillo linked an issue Aug 13, 2024 that may be closed by this pull request
@rjmurillo rjmurillo marked this pull request as ready for review August 14, 2024 18:46
@rjmurillo rjmurillo merged commit 0d3639d into release/v0.1.0 Aug 14, 2024
10 of 11 checks passed
@rjmurillo rjmurillo deleted the feature/issue-29-prefer-formattablestring-for-culture-specific-strings branch August 14, 2024 18:46
rjmurillo added a commit that referenced this pull request Aug 19, 2024
This pull request introduces a new Roslyn analyzer and associated code
fix provider to enforce best practices for culture-specific string
formatting in C#. The analyzer identifies cases where
`FormattableString` or `string.Create` should be used instead of string
for interpolated strings, depending on the target framework version. The
code fix provider automatically suggests the appropriate replacement.

## Changes

- Analyzer: `FormattableStringForCultureSpecificStringsAnalyzer`
- Detects culture-specific string usage and recommends either
`FormattableString`, `string.Create`, or `string.Format` based on the
.NET version.
- Code Fix Provider:
`FormattableStringForCultureSpecificStringsCodeFixProvider`
  - Provides automated fixes based on the analyzer's recommendations.
- Tests: Comprehensive unit tests covering various scenarios, including
different .NET versions and complex string interpolation cases.

This analyzer aligns modern C# and .NET practices, particularly Stephen
Toub's guidance on using `string.Create` in .NET 6+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzers build dependencies Pull requests that update a dependency file documentation Improvements or additions to documentation enhancement New feature or request .NET
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Item 5: Prefer FormattableString for Culture-Specific strings
1 participant